//
//  BHProgressView.m
//  Behing
//
//  Created by Lj on 2017/10/16.
//  Copyright © 2017年 lj. All rights reserved.
//

#define Text_Width 30
#define Progress_height 6

#import "BHProgressView.h"

@interface BHProgressView ()
@property (nonatomic, strong) CALayer *backLayer;
@property (nonatomic, strong) CALayer *progressLayer;
@property (nonatomic, strong) UILabel *percentLabel;
@property (nonatomic, strong) CAGradientLayer *grain;
@property (nonatomic, strong) CABasicAnimation *animation;
@end

@implementation BHProgressView

#pragma mark - Lazy
- (UILabel *)percentLabel {
    if (_percentLabel == nil) {
        _percentLabel = [[UILabel alloc]init];
        _percentLabel.textColor = [UIColor whiteColor];
        _percentLabel.font = [UIFont systemFontOfSize:9];
        _percentLabel.textAlignment = NSTextAlignmentCenter;
        _percentLabel.bounds = CGRectMake(0, 0, Text_Width, Progress_Bar_Height);
        _percentLabel.layer.cornerRadius = Progress_Bar_Height/2;
        _percentLabel.backgroundColor = [UIColor colorWithHexString:@"#05F9BB"];
        _percentLabel.layer.borderWidth = BHBorder_Width;
        _percentLabel.clipsToBounds = YES;
        _percentLabel.layer.borderColor = [UIColor colorWithHexString:@"#03E6AC"].CGColor;
    }
    return _percentLabel;
}

- (CALayer *)backLayer {
    if (_backLayer == nil) {
        _backLayer = [[CALayer alloc]init];
        _backLayer.backgroundColor = [UIColor colorWithHexString:@"#EFEFEF "].CGColor;
        _backLayer.cornerRadius = Progress_Bar_Height/4;
        [self.layer addSublayer:_backLayer];
    }
    return _backLayer;
}

- (CALayer *)progressLayer {
    if (_progressLayer == nil) {
        _progressLayer = [[CALayer alloc]init];
        _progressLayer.backgroundColor = [UIColor colorWithHexString:@"#1DD4A6 "].CGColor;
        _progressLayer.cornerRadius = Progress_Bar_Height/4;
        [self.backLayer addSublayer:_progressLayer];
    }
    return _progressLayer;
}

- (CAGradientLayer *)grain {
    if(_grain == nil) {
        _grain = [[CAGradientLayer alloc] init];
        _grain.frame = self.bounds;
        [_grain setColors:[NSArray arrayWithObjects:
                           (id)[Main_Color CGColor],
                           (id)[[UIColor colorWithHexString:@"20C99E" alpha:0.0] CGColor],
                           nil]];
//        [_grain setLocations:@[@0, @1]];
        [_grain setStartPoint:CGPointMake(0, 0)];
        [_grain setEndPoint:CGPointMake(1, 1)];
        _grain.type = kCAGradientLayerAxial;
        [self.layer addSublayer:_grain];
    }
    return _grain;
}

- (CABasicAnimation *)animation {
    if (_animation == nil) {
        _animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        _animation.fromValue = 0;
        _animation.toValue = @(4 * M_PI);
        _animation.removedOnCompletion = NO;
        _animation.repeatCount = NSIntegerMax;
        _animation.duration = 3;
    }
    return _animation;
}

#pragma mark - init
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {

    }
    return self;
}

- (id)init {
    if (self = [super init]) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
}

- (void)_turnRingsetUpUI {
    
    CGFloat bgRadius = CGRectGetWidth(self.bounds)/2 - 2;
    CAShapeLayer *backgroundLayer = [CAShapeLayer layer];
    backgroundLayer.frame = self.bounds;
    backgroundLayer.fillColor = [UIColor clearColor].CGColor;
    backgroundLayer.strokeColor = [UIColor redColor].CGColor;
    backgroundLayer.lineWidth = 3;
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(bgRadius + 2, bgRadius + 2) radius:bgRadius startAngle:- M_PI_2 endAngle: 1.3 *M_PI clockwise:YES];
    backgroundLayer.path = bezierPath.CGPath;
    [self.layer addSublayer:backgroundLayer];
    [self.grain setMask:backgroundLayer];
    self.backLayer = backgroundLayer;
}

- (void)drawRect:(CGRect)rect {
    if (self.progressType == StripProgressType) {
        [self addSubview:self.percentLabel];
        self.backLayer.frame = CGRectMake(0, (CGRectGetHeight(self.bounds) - Progress_height)/2, CGRectGetWidth(self.bounds), Progress_height);
    }
}

- (void)accordingAnimation {
    [self _turnRingsetUpUI];
    [self.backLayer addAnimation:self.animation forKey:nil];
}

- (void)hideAnimation {
    [self.backLayer removeAllAnimations];
    [self removeFromSuperview];
}

#pragma mark - Set
- (void)setIsPanGesture:(BOOL)isPanGesture {
    if (isPanGesture) {
        self.percentLabel.userInteractionEnabled = YES;
        //拖动事件
        UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]init];
        [panRecognizer addTarget:self action:@selector(panClick:)];
        [self.percentLabel addGestureRecognizer:panRecognizer];
    }else {
        self.percentLabel.userInteractionEnabled = NO;
    }
}

- (void)percentages:(CGFloat)percent animated:(BOOL)animated {
    self.percentLabel.text = [NSString stringWithFormat:@"%.f%@",percent * 100,@"%"];
    if (animated) {
        [UIView animateWithDuration:1 animations:^{
            self.progressLayer.frame = CGRectMake(0, 0, 300, Progress_height);
            self.percentLabel.center = CGPointMake(CGRectGetWidth(self.progressLayer.bounds), Progress_Bar_Height/2);
        }];
    }else {
        CGFloat width = 0.0, percentLeft = 0.0, allWidth = App_Frame_Width - 30;
        percentLeft = percent * allWidth;
        if (percentLeft < Text_Width/2) width = Text_Width/2;
        else if (percentLeft >= allWidth) width = allWidth - Text_Width/2;
        else width = percentLeft;
        self.progressLayer.frame = CGRectMake(0, 0, percent * allWidth, Progress_height);
        self.percentLabel.center = CGPointMake(width, Progress_Bar_Height/2);
    }
}

#pragma mark - UIPanGestureRecognizer(拖动)
- (void)panClick:(UIPanGestureRecognizer *)gestureRecognizer {
    //获取拖动按钮的坐标
    CGPoint point = [gestureRecognizer translationInView:self.percentLabel];
    gestureRecognizer.view.center = CGPointMake(gestureRecognizer.view.center.x + point.x, Progress_Bar_Height/2);
}


@end
